//
//  HWsousujiemiankongzhiqiViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/25.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWsousujiemiankongzhiqiViewController.h"

#import "HWlibaozhongCollectionViewCell.h"
#import "HWlibaomoxingModel.h"
#import "HWGiftBaglibaoxiangqingViewController.h" 

@interface HWsousujiemiankongzhiqiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
@property(weak,nonatomic) UITextField *searchTextField;
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@end

@implementation HWsousujiemiankongzhiqiViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupNavigation];
    [self addControlsview];
}




#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    
    CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
//    layout.minimumInteritemSpacing = juanju-1;
    layout.minimumInteritemSpacing  = 1;
    
    
  //   layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, kScreenW, kScreenH-NavBarHeight-44) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
   // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
 //   [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
   [self.maincollectionView registerClass:[HWlibaozhongCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
  //   [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    
    
}



#pragma mark ------- 设置导航栏
- (void)setupNavigation {
    

    
    UIView * naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, StatusBarHeight+44)];
   // naviView.backgroundColor = [UIColor redColor];
    [self.view addSubview:naviView];
    
    
  
    UITextField *texfeild = [[UITextField alloc]initWithFrame:CGRectMake(15, StatusBarHeight+ 7, kScreenW-15-55, 30)];
    texfeild.returnKeyType =UIReturnKeySearch;
    texfeild.delegate = self;
    texfeild.placeholder = @"输入你要搜索的游戏";
    texfeild.placeholder  =  [HWjianzhiduiObject keylocalization:@"输入你要搜索的游戏"];
    
    //    texfeild.layer.borderColor  = RGB(208, 208, 208).CGColor;
    //    texfeild.layer.borderWidth = 0.5;
//    texfeild.font = [UIFont systemFontOfSize:12];
//    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 0, 30, 30);
//    [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateHighlighted];
    
    UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 32)];
        UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(5, 0, 30, 30);
        [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateHighlighted];
       [sousuoView addSubview:button];
    
    
    texfeild.leftView = sousuoView;
    texfeild.leftViewMode = UITextFieldViewModeAlways;
    texfeild.layer.cornerRadius = 15;
    texfeild.layer.masksToBounds = YES;
    texfeild.backgroundColor = RGB(245, 245, 245);
    //   [texfeild becomeFirstResponder];
    [naviView addSubview:texfeild];
    self.searchTextField = texfeild;
    
     UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
     cancelButton.frame = CGRectMake(kScreenW-55, StatusBarHeight, 50, 44);
     [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
     [cancelButton setTitle:@"取消" forState:UIControlStateHighlighted];
    
    
    [cancelButton setTitle:[HWjianzhiduiObject keylocalization:@"取消"] forState:UIControlStateNormal];
    [cancelButton setTitle:[HWjianzhiduiObject keylocalization:@"取消"] forState:UIControlStateHighlighted];
    
    
    
     [cancelButton setTitleColor:RGB(51, 52,49) forState:UIControlStateNormal];
     [cancelButton setTitleColor:RGB(51, 52,49) forState:UIControlStateHighlighted];
    
    
     [cancelButton addTarget:self action:@selector(cancelButtonTap) forControlEvents:UIControlEventTouchUpInside];
     [naviView addSubview:cancelButton];
    

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"12345");
    
      [self searchRutanjieguo];
    
    
    
    return  YES;
    
    
}

- (void)cancelButtonTap {
    
    
    [self.searchTextField canResignFirstResponder];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    
}


- (void) searchRutanjieguo {
    
    self.dataMuArr = [[NSMutableArray alloc]init];
      
    if (!(self.searchTextField.text.length > 0)) {
        
        [MBProgressHUD showError:@"请输入搜索内容"];
        return;
    }
      
    
      
     //  NSString *typeStr = @"1";
      
      
    [MBProgressHUD show:@"正在搜索"];

    
    
      NSDictionary *parametes = @{
                                  @"ct" :  @"app",
                                  @"ac" : @"lb",
                                  @"p"  : @"1",
                                  @"ios" : @"1",
                                  @"type" : self.typeStr,
                                  @"member_id":[HWLocallyStoredData getCode],
                                  @"keyword":self.searchTextField.text,
                                  
                                  };
      
      
    
      
      [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
          
          
          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
         
          [self.maincollectionView.mj_header endRefreshing];
          [self.maincollectionView.mj_footer endRefreshing];
          
          [MBProgressHUD hideHUD];
          
        
          
          
          if ([codeStr intValue] == 1) {
            //  [MBProgressHUD  showError:@"加载成功"];
                  NSArray *arr = response[@"data"];
            
                 self.dataMuArr = [HWlibaomoxingModel mj_objectArrayWithKeyValuesArray:arr];
                  
//                  self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
//                  
//                  self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
//                  
                  [self.maincollectionView reloadData];
               
                  


            
              
              
          } else {
              
              self.dataMuArr = [[NSMutableArray alloc]init];
              
              [MBProgressHUD showError:response[@"message"]];
              
              
              
              [self.maincollectionView reloadData];
           
              
          }
          NSLog(@"%@",response);
          
          
      } failure:^(NSString *error) {
          
          [self.maincollectionView.mj_header endRefreshing];
          [self.maincollectionView.mj_footer endRefreshing];
         
          
          
      }];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return self.dataMuArr.count;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWlibaozhongCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
  //   cell.backgroundColor =[UIColor yellowColor];
   //  HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
   //  cell.model = mode;
    
    
   HWlibaomoxingModel *model =       self.dataMuArr[indexPath.row];
    [cell.tupianicImageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:nil];
    cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",model.name];
    cell.libaoshulian.text = [NSString stringWithFormat:@"礼包数：%@",model.lb_num];
    
    
    
    if (indexPath.row%2== 0) {
        cell.backgroundColor = [UIColor whiteColor];
        
        
    } else {
        
         cell.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    
    
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenW-2)/2,62);
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    
      HWlibaomoxingModel *model =   self.dataMuArr[indexPath.row];
     HWGiftBaglibaoxiangqingViewController *gameDetails = [[HWGiftBaglibaoxiangqingViewController alloc]init];

     gameDetails.gidStr = model.gid;

     [self.navigationController pushViewController:gameDetails animated:YES];
    
    
}



@end
